package com.tyj.tuyijieapp.config.security;


import com.tyj.tuyijieapp.config.security.captcha.CaptchaAuthenticationProcessingFilter;
import com.tyj.tuyijieapp.config.security.captcha.CaptchaProvider;
import com.tyj.tuyijieapp.config.security.handler.AccessHandler;
import com.tyj.tuyijieapp.config.security.handler.UnLoginHandler;
import com.tyj.tuyijieapp.config.security.password.PasswordFilter;
import com.tyj.tuyijieapp.config.security.password.PasswordProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 * <p></p>
 *
 * @author lhf
 * @since 2020/6/6
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private CaptchaProvider captchaProvider;
    @Resource
    private AccessHandler accessHandler;
    @Resource
    private UnLoginHandler unLoginHandler;
    @Resource
    private CaptchaAuthenticationProcessingFilter captchaAuthenticationProcessingFilter;
    @Resource
    private PasswordProvider passwordProvider;
    @Resource
    private PasswordFilter passwordFilter;
    @Resource
    private JwtAuthenticationFilter jwtAuthenticationFilter;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(passwordProvider);
        auth.authenticationProvider(captchaProvider);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/swagger-ui.html",
                "/swagger-resources/**",
                "/images/**",
                "/webjars/**",
                "/v2/api-docs",
                "/configuration/ui",
                "/configuration/security",
                "/j-user/register",
                "/j-user/code");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .antMatchers("/login/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .and()
                .exceptionHandling()
                .accessDeniedHandler(accessHandler)
                .authenticationEntryPoint(unLoginHandler)
                .and()
                .cors()
                .and()
                .csrf()
                .disable()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);


        //替换原本的登录方式
        http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
        http.addFilterBefore(captchaAuthenticationProcessingFilter, UsernamePasswordAuthenticationFilter.class);
        http.addFilterAt(passwordFilter, UsernamePasswordAuthenticationFilter.class);

    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
